package com.cn.action;

import com.alibaba.fastjson.JSON;
import com.cn.entity.UserAddEntity;
import com.cn.entity.UserEntity;
import com.cn.service.UserAddService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;


public class ShowUserAddAction implements ActionInterface {
    UserAddService userAddService=new UserAddService();
    String id=null;
    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, ClassNotFoundException {
       UserEntity userEntity=(UserEntity)request.getSession().getAttribute("userent");
        if(userEntity.getUser_id()==null){
            return "login.jsp";
        }
        id=userEntity.getUser_id();
        String op=request.getParameter("op");
        String type=request.getParameter("type");
        System.out.println(op);
        /*从页面传过来要设置为默认的对象*/
        if(op.equals("show")){//显示当前用户的地址
            return this.getList();
        }else if (op.equals("del")){//删除当前用户所选择的地址
            UserAddEntity entity=new UserAddEntity();
            String addId=request.getParameter("add_id");
            entity.setAdd_id(addId);
            int update=this.Update(entity,"del");
            return "udai_address.jsp";
        }else if(op.equals("new")){//新建用户
            /*从页面传过来的需要添加的对象*/
            String [] check=request.getParameterValues("checkbox");
            String users_name=request.getParameter("user_name");
            String users_add=request.getParameter("users_add");
            String users_phone=request.getParameter("users_phone");
            String users_add_info=request.getParameter("users_add_info");
            UserAddEntity entity=new UserAddEntity();
            entity.setUser_id(id);
            entity.setUsers_add(users_add);
            entity.setUsers_name(users_name);
            entity.setUsers_phone(users_phone);
            entity.setUsers_add_info(users_add_info);
            if (check==null){
                int newAdd=this.Useradd(entity,"nodf");
            }else {
                System.out.println("5555");
                int newAdd=this.Useradd(entity,"df");
            }
            return "udai_address.jsp";
        }else if (op.equals("set")){//设置用户的默认地址
            UserAddEntity entity=new UserAddEntity();
            String addId=request.getParameter("add_id");
            entity.setUser_id(id);
            entity.setAdd_id(addId);
            int update=this.Update(entity,"df");
            return "udai_address.jsp";
        }else if (op.equals("edit")){//将用户的地址信息存入session
            String addId=request.getParameter("add_id");
            request.getSession().setAttribute("www",userAddService.queryOne(addId));
            return "udai_address_edit.jsp";
        }else if(op.equals("editInfo")){
            /*从页面传过来的需要编辑的对象*/
            String [] check=request.getParameterValues("checkbox");
            String users_add_id=request.getParameter("add_id");
            String users_name=request.getParameter("users_name");
            String users_add=request.getParameter("users_add");
            String users_phone=request.getParameter("users_phone");
            String users_add_info=request.getParameter("users_add_info");
            UserAddEntity entity=new UserAddEntity();
            entity.setAdd_id(users_add_id);
            entity.setUser_id(id);
            entity.setUsers_add(users_add);
            entity.setUsers_name(users_name);
            entity.setUsers_phone(users_phone);
            entity.setUsers_add_info(users_add_info);
            if (check==null){
                int edit=this.Update(entity,"ed");
            }else {
                int edit=this.Update(entity,"eddf");
            }
            return "udai_address.jsp";
        }else{
            return null;
        }

    }


    public String getList(){//查看全部地址信息
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(JSON.toJSONStringWithDateFormat(userAddService.queryAll(id),"yyyy-MM-dd"));
        return stringBuffer.toString() ;
    }

    public int Useradd(UserAddEntity entity, String type){//添加用户
        int c = 0;
        try {
            System.out.println("ss");
             c=  userAddService.add(entity,type);
            return c;
        }catch (Exception e){
            e.printStackTrace();
        }
        return c;
    }
    public int Update(UserAddEntity entity, String type){//修改为默认
        int c = 0;
        try {
            c=  userAddService.update(entity,type);
            return c;
        }catch (Exception e){
            e.printStackTrace();
        }
        return c;
    }
}
